﻿/*
* waterfall 1.0.2 - jQuery Plugin
* fmtxc@qq.com
* Copyright (c) 2011 qinnailin.cn
* Licensed under MIT and GPL
* Date:2012-03-20
* http://qinnailin.cn
*/
(function ($) {
     $.fn.waterfall = function (options) {
         var waterfallDiv = $(this);
         var lastline = [];
         var ops = $.extend({
             marginleft: 10,
             margintop: 5,
             childbox:"ul li",
             wffn: function () { },
             float:"left"
         }, options || {});
         var init = {
             sortWaterfall: function () {
                 var li = waterfallDiv.find(ops.childbox);
                 var liwidth = ops.marginleft + (li.get(0).offsetWidth); //获取单个的宽度
                 var boxwidth = waterfallDiv.width(); //document.documentElement.offsetWidth;//屏幕宽度
                 var oneline = parseInt(boxwidth / liwidth); //每行个数
                 var workcount = li.length;
                 for (var i = 0; i < oneline; i++) {
                     lastline[i] = 0;
                 }
                 for (var i = 0; i < workcount; i++) {
                     if (i < oneline) { //第一行
                         li.eq(i).css("top", 0);
                         if (ops.float == "left") {
                             var h = li[i].offsetHeight;
                             li.eq(i).css("left", i * liwidth);
                             lastline[i] = h + ops.margintop;
                         } else {
                             if (i > 0) {
                                 li.eq(i).css("left", (i - 1) * liwidth);
                                 h = li[i].offsetHeight;
                                 lastline[i-1] = h + ops.margintop;
                             } else {
                                 li.eq(0).css("left", (oneline - 1) * liwidth);
                                 h = li[0].offsetHeight;
                                 lastline[oneline - 1] = h + ops.margintop;
                             }
                         }
                     } else {
                         var h = li[i].offsetHeight;
                         var min_t = Math.min.apply(null, lastline);
                         var min_k = getminTopkey(lastline, min_t);
                         li.eq(i).css("top", min_t);
                         li.eq(i).css("left", min_k * liwidth);
                         lastline[min_k] += h + ops.margintop;
                     }
                 }
                 waterfallDiv.css("height", Math.max.apply(null, lastline) + ops.margintop+10);
             }
         }

         function getminTopkey(as, min_t) {
             for (var k in as) {
                 if (as[k] == min_t) {
                     return k;
                 }
             }
         }

         //window.onload = function () { init.sortWaterfall(); }
         window.onresize = function () {
             init.sortWaterfall();
         }

         function onScroll(event) {
             var closeToBottom = ($(window).scrollTop() + $(window).height() > $(document).height() - 200);
             if (closeToBottom) {
                 ops.wffn();
             }
         };

         $(document).ready(function () {
             init.sortWaterfall();
             $(window).bind('scroll', onScroll);
         });
         return init;
     }
 })(jQuery)